package com.cacf.uddi.clientExample;

import inspireit.uddi.BindingTemplate;
import inspireit.uddi.BusinessService;
import inspireit.uddi.base.AccessPoint;
import inspireit.uddi.base.BindingKey;
import inspireit.uddi.base.BusinessKey;
import inspireit.uddi.base.Name;
import inspireit.uddi.base.ServiceKey;
import inspireit.uddi.base.TModelInstanceDetails;
import inspireit.uddi.base.TModelInstanceInfo;
import inspireit.uddi.base.TModelKey;
import inspireit.uddi.base.UDDIConstants;
import inspireit.uddi.base.UDDIException;
import inspireit.uddi.collections.BindingTemplates;
import inspireit.uddi.collections.BusinessServices;
import inspireit.uddi.collections.Names;
import inspireit.uddi.collections.Results;
import inspireit.uddi.request.UDDIProfile;
import inspireit.uddi.request.UDDIPublisher;
import inspireit.uddi.response.DispositionReport;
import inspireit.uddi.response.ServiceDetail;
import inspireit.uddi.response.base.Result;

import java.net.URL;

/**
 * This example shows how to save a service. You will have to open an account at
 * one of the official UDDI registries if you don't have access to a local UDDI
 * registry. For example, you can use the beta registry of IBM located at
 * http://uddi.ibm.com/beta/registry.html.
 * <p>
 * 
 * The example first saves a business entity via the SaveBusinessExample
 * example. If the size of your account is limited, you will have to make a
 * clean-up first, for example via the Web interface of the on-line registry.
 * 
 * @author Bertrand Fontaine, <a href="http://www.inspireit.biz"
 *         target="INSPIREIT">INSPIRE IT</a>
 */
public class SaveServiceExample {

	protected BusinessKey businessKey = null;
	protected ServiceKey serviceKey = null;

	public SaveServiceExample(String queryURL, String publishURL, String securityURL, String username, String password)throws Exception {
		System.out.println("saveService");
		// first invoke the SaveBusiness example to create a parent
		// BusinessEntity to the BusinessService we are going to create and save
		SaveBusinessExample saveBusiness = new SaveBusinessExample(queryURL, publishURL, securityURL, username, password);
		businessKey = saveBusiness.getBusinessKey();

		// now we can safely start our example by defining our profile
		System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
		java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		UDDIProfile profile = new UDDIProfile();
		profile.setQueryURL(new URL(queryURL));
		profile.setPublishURL(new URL(publishURL));

		if (securityURL != null) {
			profile.setSecurityURL(new URL(securityURL));
		}

		profile.setUsername(username);
		profile.setPassword(password);

		// uncomment the following line to use UDDI V2 or V3 messaging instead
		// of the default V1
		// profile.setVersion(UDDIConstants.UDDI_V2);
		 profile.setVersion(UDDIConstants.UDDI_V3);

		try {
			// get an appropriate publisher for a UDDI V1-compatible registry
			UDDIPublisher publisher = UDDIPublisher.getPublisher(profile);

			// build up the BusinessEntity to save in the registry
			Name name = new Name("INSPIRE IT UDDI - Sample Service");
			Names names = new Names();
			names.add(name);
			
			ServiceKey serviceKey = null; // it's a new service...
			BindingKey bindingKey = null; // we are saving
			AccessPoint accessPoint = new AccessPoint("http://www.mysite.com/myaccesspoint", AccessPoint.HTTP);
			TModelInstanceDetails tModelInstanceDetails = new TModelInstanceDetails(); // mandatory node
			
			BindingTemplate bindingTemplate = new BindingTemplate(bindingKey, serviceKey, accessPoint, tModelInstanceDetails);
			BindingTemplates bindingTemplates = new BindingTemplates();
			bindingTemplates.add(bindingTemplate);
			
			

			BusinessService businessService = new BusinessService(serviceKey, businessKey, names, bindingTemplates);
			
			
			
			// execute the SaveService operation and get back the ServiceDetail
			ServiceDetail serviceDetail = publisher.saveService(businessService);
			
			
//			System.out.println("totooooooooooo");
			
			// get the saved BusinessService
			BusinessServices businessServices = serviceDetail.getBusinessServices();
			BusinessService savedBusinessService = (businessServices.toArray())[0];

			this.serviceKey = savedBusinessService.getServiceKey();
			System.out.println("BusinessService saved with service key : "+ this.serviceKey);

		} catch (UDDIException e) {
			
			System.out.println("An error has occured. More information follows.");
			
			DispositionReport dispositionReport = e.getDispositionReport();
			
			Results results = dispositionReport.getResults();
			
			Result result[] = results.toArray();

			for (int i = 0; i < result.length; i++) {
				System.out.println("Errno   : " + result[i].getErrNo() + "\n KeyType : " + result[i].getKeyType() + "\n ErrInfo : " + result[i].getErrInfo());
			}
		}
	}

	public ServiceKey getServiceKey() {
		return serviceKey;
	}

	public BusinessKey getBusinessKey() {
		return businessKey;
	}

	public static void main(String args[]) throws Exception {
//		if ((args.length != 4) && (args.length != 5)) {
//			System.out
//					.println("SaveServiceExample usage: java SaveServiceExample <queryURL> <publishURL> [<securityURL>] <username> <password>");
//			System.exit(0);
//		}

		String queryURL    = "http://localhost:8080/juddiv3/services/inquiry"; 
        String publishURL  = "http://localhost:8080/juddiv3/services/publish"; 
        String securityURL = "http://localhost:8080/juddiv3/services/security"; 
        String username    = "root"; 
        String password    = "root";

//		if (args.length == 4) {
//			username = args[2];
//			password = args[3];
//		} else { // must be 5 ...
//			securityURL = args[2];
//			username = args[3];
//			password = args[4];
//		}

		new SaveServiceExample(queryURL, publishURL, securityURL, username, password);
		return;
	}
}